package com.amigo.online.provider.adv.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

import com.amigo.online.common.param.result.AdvAreaInfo;
import com.amigo.online.provider.adv.entity.AdvArea;

public class AdvBasicUtil {

    /**
     * 根据固定格式生成redis key
     *
     * @param basic
     * @param cityId
     * @param sexType
     * @param ageType
     * @return
     */
    public static String spliceAdvRedisKey(String basic, Integer cityId, Integer sexType, Integer ageType) {
        StringBuilder sb = new StringBuilder().append(basic);
        if (cityId != null) {
            sb.append(":").append(cityId);
        } else {
            //0代表全部区域
            sb.append(":").append(0);
        }

        if (sexType != null) {
            sb.append(":").append(sexType);
        } else {
            sb.append(":").append(0);
        }

        if (ageType != null) {
            sb.append(":").append(ageType);
        } else {
            sb.append(":").append(0);
        }

        return sb.toString();
    }

    /**
     * @param age
     */
    public static int userAgeTransferToLabel(int age) {
        if (age >= 18 && age <= 23) {
            return 1;
        } else if (age >= 24 && age <= 40) {
            return 2;
        } else if (age > 40) {
            return 3;
        }
        return 0;
    }

    /**
     * 转化用户的投放广告的年龄选项
     *
     * @param numer
     * @param result
     * @return
     */
    public static List<Integer> tansferAgeTypeToList(int numer, List<Integer> result) {
        int index = 0;
        while (numer > 0) {
            int flag = numer % 10;
            if (flag == 1) {
                result.add(index);
            }
            index++;
            numer /= 10;
        }

        return result;
    }

    /**
     * @return
     */
    public static long getTodayZeroPointTimeStamps() {
        long currentTimeStamps = System.currentTimeMillis();
        long oneDayTimeStamps = 1000 * 24 * 60 * 60;
        return (currentTimeStamps - (currentTimeStamps + oneDayTimeStamps) % oneDayTimeStamps) / 1000;
    }

    public static void main(String[] args) {
        System.out.println(getTomorrowZeroPointTimeStamps());
    }

    /**
     * @return
     */
    public static long getTomorrowZeroPointTimeStamps() {
        long oneDayTimeStamps = 1000 * 24 * 60 * 60;

        long currentTimeStamps = Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTimeInMillis();
	/*	Calendar cal = Calendar.getInstance();
		cal.get(Calendar.ZONE_OFFSET);
		cal.get(Calendar.DST_OFFSET);
			
		long currentTimeStamps =  Calendar.getInstance().getTimeInMillis();
		*/
        return (currentTimeStamps + oneDayTimeStamps - (currentTimeStamps + oneDayTimeStamps) % oneDayTimeStamps) / 1000;
    }


    public static List<AdvAreaInfo> AdvAreaToAdvAreaInfo(List<AdvArea> list, String language) {
        if (list == null) {
            return new ArrayList<AdvAreaInfo>();
        }
        List<AdvAreaInfo> advList = new ArrayList<>();
        for (AdvArea advArea : list) {
            if (advArea.getLevel().equals(1)) {
                AdvAreaInfo adv = new AdvAreaInfo();
                adv.setId(advArea.getId());
                adv.setLevel(advArea.getLevel());
                getLanguage(advArea, adv, language);
                adv.setPid(advArea.getPid());
                adv.setSort(advArea.getSort());
                advList.add(adv);
            }
        }
        for (AdvAreaInfo advAreaInfo : advList) {
            Long pid = advAreaInfo.getId();
            //每个州下面有个全部
            AdvAreaInfo quanbu = new AdvAreaInfo();
            quanbu.setId(pid);
            quanbu.setLevel(1);
            getLanguage(quanbu, language);
            quanbu.setPid(0l);
            quanbu.setSort(0);
            advAreaInfo.getList().add(quanbu);
            for (AdvArea advArea : list) {
                if (advArea.getPid().equals(pid)) {
                    AdvAreaInfo adv = new AdvAreaInfo();
                    adv.setId(advArea.getId());
                    adv.setLevel(advArea.getLevel());
                    getLanguage(advArea, adv, language);
                    adv.setPid(advArea.getPid());
                    adv.setSort(advArea.getSort());
                    advAreaInfo.getList().add(adv);
                }
            }
            advAreaInfo.Sort();
        }
        return advList;
    }


    private static void getLanguage(AdvArea advArea, AdvAreaInfo adv, String language) {
        switch (language) {
            case "name_cn":
                adv.setName(advArea.getNameCn());
                break;
            case "name_pt":
                adv.setName(advArea.getNamePt());
                break;
            case "name_es":
                adv.setName(advArea.getNameEs());
                break;
            case "name_en":
                adv.setName(advArea.getNameEn());
                break;
            default:
                adv.setName(advArea.getNamePt());
                break;
        }
    }

    private static void getLanguage(AdvAreaInfo adv, String language) {
        switch (language) {
            case "name_cn":
                adv.setName("Todos");
                break;
            case "name_pt":
                adv.setName("Todos");
                break;
            case "name_es":
                adv.setName("all");
                break;
            case "name_en":
                adv.setName("all");
                break;
            default:
                adv.setName("Todos");
                break;
        }
    }

}
